package com.tinckay.controller;


import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.tinckay.common.annotation.Log;
import com.tinckay.common.constant.ProjectConstant;
import com.tinckay.common.constant.RegexConstant;
import com.tinckay.entity.po.Menu;
import com.tinckay.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.api.ApiController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-10-30
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController extends ApiController {

    @Autowired
    private MenuService menuService;

    @Log("新增菜单信息")
    @PostMapping
    @ApiOperation("新增菜单信息")
    //@RequiresPermissions("menu:add")
    public R add(@RequestBody @Validated(ProjectConstant.Save.class) Menu menu) {
        menuService.addMenu(menu);
        return success(null);
    }

    @Log("删除菜单信息")
    @DeleteMapping
    @ApiOperation("删除菜单信息")
    //@RequiresPermissions("menu:delete")
    public R delete(@ApiParam("菜单信息id数组") @RequestParam Long[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return failed("参数id不能为空.");
        }
        menuService.deleteMenus(ids);
        return success(null);
    }

    @Log("更新菜单信息")
    @PutMapping
    @ApiOperation("更新菜单信息")
    //@RequiresPermissions("menu:update")
    public R update(@RequestBody @Validated(ProjectConstant.Update.class) Menu menu) {
        if (null == menu.getParentId()) {
            menu.setParentId(0L);
        }
        menuService.updateById(menu);
        return success(menu);
    }

    @GetMapping("/list")
    @ApiOperation("查询菜单信息列表")
    //@RequiresPermissions("menu:list")
    public R list(@RequestParam(required = false) String menuName,
                  @RequestParam(required = false) String type) {
        boolean condition = false;
        if (StrUtil.isNotBlank(type)) {
            if (type.matches(RegexConstant.ZERO_OR_ONE)) {
                return failed("菜单类型有误");
            } else {
                condition = true;
            }
        }
        List<Menu> result = menuService.list(
                new QueryWrapper<Menu>()
                        .lambda()
                        .like(StrUtil.isNotBlank(menuName), Menu::getMenuName, menuName)
                        .eq(condition, Menu::getType, type)
        );
        return success(result);
    }

    @GetMapping("/info/{menuId}")
    @ApiOperation("获取菜单明细信息")
    //@RequiresPermissions("menu:info")
    public R<Menu> info(@PathVariable("menuId") long menuId) {
        return success(menuService.getById(menuId));
    }

    @GetMapping("/findUserMenus/{username}")
    @ApiOperation("获取用户资源列表")
    //@RequiresPermissions("menu:findUserMenus")
    public R findUserMenus(@PathVariable("username") String username) {
        List<Menu> menus = menuService.findUserMenus(username);
        return success(menus);
    }
}

